/*
 * Copyright 2010 Christoph Widulle (christoph.widulle@googlemail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */



package com.android1.amarena2d.input;

import com.android1.amarena2d.commons.collections.FixedSizeArray;
import com.android1.amarena2d.engine.AmarenaCamera;
import com.android1.amarena2d.engine.EngineObject;
import com.android1.amarena2d.engine.TouchSpotSystem;

public abstract class BaseTouchSpot extends EngineObject implements TouchSpot {

    protected boolean active = true;
    protected int priority;
    private boolean translate = true;
    private boolean notifyNoHits;
    private boolean notifyAllListener = false;

    private FixedSizeArray<TouchListener> listenerList;

    protected  static int nextPriority(){
        return TouchSpotSystem.nextPriority();
    }

    protected BaseTouchSpot() {
        this.priority = nextPriority();
    }

    protected BaseTouchSpot(int priority) {
        this.priority = priority;
    }

    public TouchListener addListener(TouchListener listener) {
        ensureListenerList();
        listenerList.add(listener);
        return listener;
    }

    public void remove(TouchListener listener) {
        if (listenerList != null)
            listenerList.remove(listener);
    }

    private void ensureListenerList() {
        if (listenerList == null)
            listenerList = new FixedSizeArray<TouchListener>(10);
    }

    public boolean isActive() {
        return active;
    }

    public void setActive(boolean active) {
        this.active = active;
    }

    public void setTranslateToWorld(boolean translate) {
        this.translate = translate;
    }

    @Override
    public int getPriority() {
        return priority;
    }

    @Override
    public boolean isTranslated() {
        return translate;
    }

    public void unregister() {
        engine.getTouchSpotSystem().unregister(this);
    }

    public void register() {
        engine.getTouchSpotSystem().register(this);
    }

    @Override
    public boolean isNotifyNoHits() {
        return notifyNoHits;
    }

    @Override
    public void setNotifyNoHits(boolean notifyNoHits) {
        this.notifyNoHits = notifyNoHits;
    }

    @Override
    public boolean isAlwaysNotifyAllListener() {
        return notifyAllListener;
    }

    @Override
    public void setAlwaysNotifyAllListener(boolean notifyAllListener) {
        this.notifyAllListener = notifyAllListener;
    }

    @Override
    public abstract AmarenaCamera getCamera() ;

    @Override
    public boolean onTouchDown(float x, float y, int pointer, boolean hit) {
        if (active) {
            int s = listenerList == null ? 0 : listenerList.getCount();
            if (s > 0) {
                boolean found = false;
                for (int i = 0; i < s; i++) {
                    TouchListener t = listenerList.get(i);
                    if (t.onTouchDown(x, y, pointer, hit)) {
                        if (!notifyAllListener)
                            return true;
                        else
                            found = true;
                    }
                }
                return found;
            }
        }
        return false;
    }

    @Override
    public boolean onTouchUp(float x, float y, int pointer, boolean hit) {
        if (active) {
            int s = listenerList == null ? 0 : listenerList.getCount();
            if (s > 0) {
                boolean found = false;
                for (int i = 0; i < s; i++) {
                    TouchListener t = listenerList.get(i);
                    if (t.onTouchUp(x, y, pointer, hit)) {
                        if (!notifyAllListener)
                            return true;
                        else
                            found = true;
                    }
                }
                return found;
            }
        }
        return false;
    }

    @Override
    public boolean onTouchDragged(float x, float y, int pointer, boolean hit) {
        if (active) {
            int s = listenerList == null ? 0 : listenerList.getCount();
            if (s > 0) {
                boolean found = false;
                for (int i = 0; i < s; i++) {
                    TouchListener t = listenerList.get(i);
                    if (t.onTouchDragged(x, y, pointer, hit)) {
                        if (!notifyAllListener)
                            return true;
                        else
                            found = true;
                    }
                }
                return found;
            }
        }
        return false;
    }


}